package com.msb.mall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.msb.common.dto.MemberLoginDTO;
import com.msb.common.dto.SocialUser;
import com.msb.common.exception.BizCodeEnume;
import com.msb.mall.member.exception.PhoneExsitException;
import com.msb.mall.member.exception.UsernameExsitException;
import com.msb.common.dto.MemberRegisterDTO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.msb.mall.member.entity.MemberEntity;
import com.msb.mall.member.service.MemberService;
import com.msb.common.utils.PageUtils;
import com.msb.common.utils.R;



/**
 * 会员
 *
 * @author dpb
 * @email dengpbs@163.com
 * @date 2022-10-08 23:01:44
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 登录
     * @param dto
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginDTO dto){
        R r = memberService.login(dto);
        return r;
    }

    /**
     * 社交平台登录
     * @param dto
     * @return
     */
    @PostMapping("/oauth/login")
    public R oauthLogin(@RequestBody SocialUser dto){
        R r = memberService.login(dto);
        return r;
    }

    /**
     * 用户注册
     * @param dto
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody MemberRegisterDTO dto){
        try {
            memberService.register(dto);
        }catch (UsernameExsitException userEx){
            return R.error(BizCodeEnume.USERNAME_EXSIT_EXCEPTION.getCode(), BizCodeEnume.USERNAME_EXSIT_EXCEPTION.getMsg());
        }catch (PhoneExsitException phoneEx){
            return R.error(BizCodeEnume.PHONE_EXSIT_EXCEPTION.getCode(), BizCodeEnume.PHONE_EXSIT_EXCEPTION.getMsg());
        }catch (Exception e){
            return R.error(BizCodeEnume.UNKNOW_EXCEPTION.getCode(), BizCodeEnume.UNKNOW_EXCEPTION.getMsg());
        }

        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
